/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ServiceController;

import com.Service.BO.DirectoratesBO;
import com.Service.DAO.SuperDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author olugbokikisemiu
 */
public class AddDirectorateController extends HttpServlet {

    SuperDAO direct;
    DirectoratesBO divex;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            direct = new SuperDAO();
            divex = new DirectoratesBO();
            
            String name = request.getParameter("dirname");
            String shortdesc = request.getParameter("desc");
            String lead = request.getParameter("leadcontact");
            String add1 = request.getParameter("addline1");
            String add2 = request.getParameter("addline2");
            String add3 = request.getParameter("addline3");
            String post = request.getParameter("postcode");
            String city = request.getParameter("city");
            String count = request.getParameter("county");
            int country = Integer.parseInt(request.getParameter("country"));
            String type = request.getParameter("type");
            String full = request.getParameter("full");
            int code =  Integer.parseInt(request.getParameter("code"));
            int fone = Integer.parseInt(request.getParameter("phone"));
            int fax = Integer.parseInt(request.getParameter("fax"));
            String mail = request.getParameter("email");
            String web = request.getParameter("webAdd");
            String active = request.getParameter("Inactive");
            int org = Integer.parseInt(request.getParameter("orgname"));
            int car = Integer.parseInt(request.getParameter("char"));
            int com = Integer.parseInt(request.getParameter("com"));
            
            divex.setName(name);
            divex.setDesc(shortdesc);
            divex.setLead(lead);
            divex.setAddline1(add1);
            divex.setAddline2(add2);
            divex.setAddline3(add3);
            divex.setPostcode(post);
            divex.setCity(city);
            divex.setCounty(count);
            divex.getCountry().setID(country);
            divex.setType(type);
            divex.setFulldesc(full);
            divex.setSicCode(code);
            divex.setFone(fone);
            divex.setFax(fax);
            divex.setEmail(mail);
            divex.setWebAddress(web);
            
            divex.setCharity(car);
            divex.setComnumber(com);
            divex.getOrg().setId(org);
            
            if(active != null)
            {
                divex.setIsActive("True");
            }
            else
            {
                divex.setIsActive("False");
            }
            
            direct.saveDirectorates(divex);
            RequestDispatcher req = request.getRequestDispatcher("SpecialismController");
            req.forward(request, response);
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
